<!DOCTYPE html>
<html>
<head>
	<title>	</title>
</head>
<body>
	<script>
        function ajax(options){
			var xhr = new XMLHttpRequest();

			var params = '';
			for(var attr in options.data){
				params += attr + '=' + options.data[attr] + '&';
			}

			params = params.substr(0,params.length - 1);

			console.log(params);

			if(options.type == 'get'){
				options.url = options.url + '?' + params;
			}
		
			xhr.open(options.type, options.url);

			if(options.type == 'post'){
				var contentType = options.herader['Content-Type']
				xhr.setRequestHeader('Content-Type', contentType);
				if(contentType == 'application/json'){
					xhr.send(JSON.stringify(options.data))
				}else{
					xhr.send(params);
				}

			}else{
				xhr.send();
			}

			// xhr.send();
			xhr.onload = function(){
				// console.log(xhr.responseText);
				var contentType = xhr.getResponseHeader('Content-Type');
				var responseText = xhr.responseText;
				if(contentType.includes('application/json')){
					var responseText = JSON.parse(responseText)

				}

				if(xhr.status == 200){
					options.success(responseText,xhr);
				}else{
					options.error(responseText, xhr)
				}
				
			}
		}

		ajax({
			type: 'get',
			url: "http://localhost:3000/responseDate",
			data: {
				name: 'zhangsan',
				age:20
			},
			herader: {
				'Content-Type': 'application/json'
			},
			success: function(data){
				console.log('这里是sussess');
				console.log(data)
			},
			error: function(data,xhr){
				console.log('这里是error函数' + data);
				console.log(xhr);
			}
		})
    </script>
</body>
</html>